package com.jjc.android.fragment

import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.annotation.IdRes
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.target.Target
import com.bumptech.glide.request.transition.Transition
import com.jjc.android.Constants
import com.jjc.android.R
import com.jjc.android.activity.AddSamplingActivity
import com.jjc.android.activity.DetectionActivity
import com.jjc.android.base.BaseLazyLoadFragment
import com.jjc.android.entity.*
import com.jjc.android.net.SimpleHttpCallback
import com.jjc.android.util.Common.parseEmpty
import com.jjc.android.util.MultiLanguageUtil
import com.jjc.android.util.paramEx
import com.jjc.android.util.showImage
import com.jjc.android.view.layout.*
import com.jjc.android.view.onClick
import com.lzy.okgo.OkGo
import kotlin.math.roundToInt

class SamplingFragment : BaseLazyLoadFragment(),OnDetectionPageListener {

    companion object{
        fun newInstance():SamplingFragment{
            val fragment = SamplingFragment()
            fragment.setInflateLazy(true)
            return fragment
        }
    }

    override fun getLayoutId(): Int = R.layout.fragment_sampling
    private val okBtn: TextView by lazy { findViewById<TextView>(R.id.okBtn) }
    private val samplingImageLayout:LinearLayout by lazy { findViewById(R.id.samplingImageLayout) }
    private val contentText:TextView by lazy { findViewById(R.id.contentText) }
    override fun initView(view: View) {
        fetchPageInfo()
        okBtn.onClick {
            AddSamplingActivity.start(requireActivity(),scanDeviceDto()?.lcq6twxkuyytwpkc.let { if(it.isNullOrEmpty()) "" else it[0].id}){
                if(it){
                    (activity as? DetectionActivity)?.nextStep()
                }
            }
        }
    }

    override fun onCurrentStepInvoke() {
        if(ismIsFirstVisible()) return
        samplingImageLayout.removeAllViews()
        fetchPageInfo()
    }

    private fun scanDeviceDto():ScanCardDto?{
        return (activity as? DetectionActivity)?.getScanCardInfo()
    }

    override fun fetchPageInfo() {
        OkGo.post<LzyResponse<PageInfo<SamplingPageDto>>>(Constants.getFormList)
            .paramEx(mutableMapOf("page" to 1,"pagesize" to 1,"needpage" to 1,"formheaduuid" to "68b6063091a54242a0d3b7794ef14529","filters" to listOf<KeyValuePair>(
                KeyValuePair("ktrpe1fzjzsdoek8",scanDeviceDto()?.lcq6twxkuyytwpkc.let { if(it.isNullOrEmpty()) "" else it[0].id}),
                KeyValuePair("vydvtklk0xjgsinm", MultiLanguageUtil.getInstance().getValue("746f5cd0-dee7-4a10-8f91-75b27ac123b4","69bd0b96-e704-4522-8543-4c140691793c")),
                KeyValuePair("xkw6hzbgrureov83",MultiLanguageUtil.getInstance().getValue("168d2a84-8725-4a57-a54f-eab24433a466","9b736682-d5af-441c-9367-06e7a24639ef"))
            )),true)
            .tag(this)
            .execute(object : SimpleHttpCallback<LzyResponse<PageInfo<SamplingPageDto>>>(){
                override fun onCallSuccess(response: LzyResponse<PageInfo<SamplingPageDto>>) {
                    response.data?.first?.jsonMember9rjnk9bcnstxhdli.let { images->
                        if(images.isNullOrEmpty()){
                            samplingImageLayout.visibility = View.GONE
                        }else{
                            samplingImageLayout.visibility = View.VISIBLE
                            samplingImageLayout.removeAllViews()
                            images.forEach { image->
                                samplingImageLayout.ImageView {
                                    layout_width = match_parent
                                    layout_height = wrap_content
                                    scaleType = ImageView.ScaleType.FIT_XY
                                    adjustViewBounds = true
                                    margin_bottom = 6
                                    showImage(image.url)
                                }
                            }
                        }
                    }
                    contentText.text = response.data?.first?.ovoqe3wroqadzqso.parseEmpty()
                }
            })
    }

    private fun <T : View?> findViewById(@IdRes id: Int): T {
        return rootView!!.findViewById<T>(id)
    }

}